#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sstream>
#include "log.hpp"

class Util
{
public:
    // 网页文件为文本，而图片视频等为二进制
    static bool Readfile(const std::string &path, std::string *fileContent)
    {
        // 获取文件本身的大小
        struct stat st;
        int n = stat(path.c_str(), &st);
        if (n < 0)
            return false;
        int size = st.st_size;
        // 调节大小
        fileContent->resize(size);

        int fd = open(path.c_str(), O_RDONLY);
        if (fd < 0)
            return false;
        read(fd, (char *)fileContent->c_str(), size);
        close(fd);

        logmessage(INFO, "read file %s done", path.c_str());
        //std::cout<<fileContent->c_str()<<std::endl;
        return true;
    }

    static std::string ReadOneLine(std::string &message, const std::string sep)
    {
        auto pos = message.find(sep);
        if (pos == std::string::npos)
            return "";
        std::string s = message.substr(0, pos);
        message.erase(0, pos + sep.size());
        return s;
    }

    static bool ParseRequestLine(const std::string &line, std::string *method, std::string *url, std::string *httpVersion)
    {
        std::stringstream ss(line);
        ss >> *method >> *url >> *httpVersion;

        return true;
    }
};